﻿// SMIL is a community project available under the Microsoft Public License (Ms-PL)
// Code is provided as is and with no warrenty – Use at your own risk
// View the project and the latest code at http://smil.codeplex.com/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Maps.MapControl;

namespace SMIL.Client.WPXAP
{
    public partial class MainPage : UserControl
    {
        const string _APPKEY = "SMIL.BingMap.CredentialsKey";
        const string _SPWEB = "SPWEB";
        const string _HOST = "HOST";

        public MainPage()
        {
            InitializeComponent();
            Loaded += new RoutedEventHandler(MainPage_Loaded);
            layerPanel.Loaded += new RoutedEventHandler(layerPanel_Loaded);
        }        

        void MainPage_Loaded(object sender, RoutedEventArgs e)
        {
            map.CredentialsProvider = new ApplicationIdCredentialsProvider((App.Current.Resources.Contains(_APPKEY)) ? App.Current.Resources[_APPKEY] as string : "");
            
            SPCommunication.Proxy.ServiceAddress = (App.Current.Resources.Contains(_HOST)) ? App.Current.Resources[_HOST] as string : "";
            setBaseSources();
        }

        void layerPanel_Loaded(object sender, RoutedEventArgs e)
        {
            layerPanel.SPSiteURI = (App.Current.Resources.Contains(_SPWEB)) ? App.Current.Resources[_SPWEB] as string : "";
            layerPanel.Connect();
        }

        private void setBaseSources()
        {
            BaseLayers.Items.Add("Road");
            BaseLayers.Items.Add("Aerial");
            BaseLayers.Items.Add("Aerial with labels");

            BaseLayers.SelectionChanged += BaseLayers_SelectionChanged;
        }

        private int baseMode;
        public int BaseMode
        {
            get
            {
                if (BaseLayers != null)
                {
                    return BaseLayers.SelectedIndex;
                }
                return baseMode;
            }
            set
            {
                baseMode = value;
                if (BaseLayers != null)
                {
                    BaseLayers.SelectedIndex = baseMode;
                }
            }
        }

        private void BaseLayers_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            setMapBaseLayer();
        }

        private void setMapBaseLayer()
        {
            if (map != null)
            {
                switch (BaseLayers.SelectedIndex)
                {
                    case 0:
                        map.Mode = new RoadMode();
                        break;
                    case 1:
                        map.Mode = new AerialMode(false);
                        break;
                    case 2:
                        map.Mode = new AerialMode(true);
                        break;
                }
            }
        }

        private void ButtonSMILLayers_Click(object sender, RoutedEventArgs e)
        {            
            fpLayerPanel.Open();         
        }

        private void ButtonFullScreen_Click(object sender, RoutedEventArgs e)
        {
            Application.Current.Host.Content.IsFullScreen = !Application.Current.Host.Content.IsFullScreen;
        }
    }
}
